package com.hnkywl.sts.dao.cwgl;

import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.sql.SQLException;


@Repository
public class VoucherDao extends SimpleHibernateTemplate {
    /**
     * 开始生产凭证
     *
     * @param id
     * @param type
     * @return
     */
    public Boolean beginGenerateVoucher(Long id, VoucherType type) {
        String sql = "update " + type.getTable() + " set jiuqi_insert_flag=2 where id =? and jiuqi_insert_flag=1";
        int updateRows = createSQLQuery(sql, id).executeUpdate();
        return updateRows == 0 ? false : true;
    }

    /**
     * 开始撤销凭证
     *
     * @param id
     * @param type
     * @return
     */
    public Boolean beginRevokeVoucher(Long id, VoucherType type) {
        String sql = "update " + type.getTable() + " set jiuqi_delete_flag=2 where id =? and jiuqi_insert_flag=3";
        int updateRows = createSQLQuery(sql, id).executeUpdate();
        return updateRows == 0 ? false : true;
    }

    /**
     * 生成凭证成功
     *
     * @param uuid
     * @param voucherId
     * @param voucherNum
     * @param acctyear
     * @param id
     * @param type
     * @return
     */
    public Boolean generateVoucherSuccess(String uuid, String voucherId, String voucherNum, Integer acctyear, Long id, VoucherType type) {
        String sql = "update " + type.getTable() + " set jiuqi_uuid=?,jiuqi_voucher_id=?,jiuqi_voucher_num=?,jiuqi_acctyear=?,jiuqi_insert_flag=3,jiuqi_delete_flag=1 where id = ? and jiuqi_insert_flag=2";
        int updateRows = createSQLQuery(sql, uuid, voucherId, voucherNum, acctyear, id).executeUpdate();
        return updateRows == 0 ? false : true;
    }

    /**
     * 撤销凭证成功
     *
     * @param id
     * @param type
     * @return
     */
    public Boolean revokeVoucherSuccess(Long id, VoucherType type) {
        String sql = "update " + type.getTable() + " set jiuqi_delete_flag=3,jiuqi_insert_flag=1 where id = ? and jiuqi_insert_flag=3 and jiuqi_delete_flag=2";
        int updateRows = createSQLQuery(sql, id).executeUpdate();
        return updateRows == 0 ? false : true;
    }

    /**
     * 重置凭证状态
     *
     * @param id
     * @param type
     * @return
     * @throws SQLException
     */
    public Boolean restVoucher(Long id, VoucherType type) {
        String flagSql = "select jiuqi_insert_flag,jiuqi_delete_flag from " + type.getTable() + " where id=? and deleted=0";
        Object[] flag = (Object[]) createSQLQuery(flagSql, id).uniqueResult();
        int updateRows = 0;
        int insertFlag = ((BigDecimal) flag[0]).intValue();
        int deleteFlag = ((BigDecimal) flag[1]).intValue();
        if (flag != null) {
            if ((insertFlag != 2 && deleteFlag != 2) || (insertFlag == 2 && deleteFlag == 2)) {
                throw new RuntimeException("该凭证不符合重置条件!");
            } else {
                if (insertFlag == 2) {
                    String sql = "update " + type.getTable() + " set jiuqi_insert_flag=1 where id=? ";
                    updateRows = createSQLQuery(sql, id).executeUpdate();
                }
                if (deleteFlag == 2) {
                    String sql = "update " + type.getTable() + " set jiuqi_delete_flag=1 where id=? ";
                    updateRows = createSQLQuery(sql, id).executeUpdate();
                }
            }
        }
        return updateRows == 0 ? false : true;
    }

    /**
     * 同步凭证状态
     *
     * @param id
     * @param type
     * @return
     * @throws SQLException
     */
    public void syncVoucher(Long id, Integer voucherNum, VoucherType type) {
        String sql = "update " + type.getTable() + " set jiuqi_voucher_num=? where id=? ";
        createSQLQuery(sql, voucherNum, id).executeUpdate();
    }
}
